package com.ruanjian.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanjian.reggie.common.R;
import com.ruanjian.reggie.dto.SetmealDto;
import com.ruanjian.reggie.entity.Category;
import com.ruanjian.reggie.entity.Setmeal;
import com.ruanjian.reggie.entity.SetmealDish;
import com.ruanjian.reggie.service.CategoryService;
import com.ruanjian.reggie.service.SetmealDishService;
import com.ruanjian.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐和菜品
     * @param setmealDto
     * @return
     */
    @PostMapping()
    public R<String> add(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealAndDish(setmealDto);
        return R.success("套餐添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        setmealService.page(setmealPage, new LambdaQueryWrapper<Setmeal>().like(name != null, Setmeal::getName, name));
        List<Setmeal> records = setmealPage.getRecords();
        List<Setmeal> collect = records.stream().map((item) -> {
            Long categoryId = item.getCategoryId();
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealPage.setRecords(collect);
        return R.success(setmealPage);
    }

    /**
     * 通过id查询套餐和套餐菜品id回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> querySetmealById(@PathVariable("id") String id){
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        List<SetmealDish> setmealDishes = setmealDishService.getBySetmealId(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    /**
     * 套餐的修改
     * @param setmealDto
     * @return
     */
    @PutMapping()
    public R<String> editSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.editSetmealDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 通过商品id更改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> setmealStatusByStatus(@PathVariable("status") int status,String ids){
        List<String> collect = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        if(status == 1){
            setmealService.update(new LambdaUpdateWrapper<Setmeal>().in(Setmeal::getId,collect).set(Setmeal::getStatus,1));
            return R.success("套餐已经起售");
        }
        else {
            setmealService.update(new LambdaUpdateWrapper<Setmeal>().in(Setmeal::getId,collect).set(Setmeal::getStatus,0));
            return R.success("套餐已经停售");
        }
    }

    /**
     * 根据id删除套餐的id
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> deleteSetmeal(String ids){
        //删除套餐关联下的套餐菜品表里面的内容
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> setmealList(String categoryId,Integer status){
        log.info("方法进来了");
//        List<Setmeal> setmeals = setmealService.getBaseMapper().selectList(new LambdaQueryWrapper<Setmeal>().eq(categoryId !=null,Setmeal::getCategoryId, categoryId).eq(status !=null,Setmeal::getStatus, status));
        List<Setmeal> list = setmealService.list(new LambdaQueryWrapper<Setmeal>().eq(categoryId != null, Setmeal::getCategoryId, categoryId).eq(status != null, Setmeal::getStatus, status));
        return R.success(list);
    }


}
